package bll

import (
	"context"
	"maya-service/store/mysql"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type supervisorWithDrawTrans struct {
	iSupervisorWithDrawTrans store.ISupervisorWithDrawTrans
}

var SupervisorWithDrawTrans = &supervisorWithDrawTrans{
	iSupervisorWithDrawTrans: mysql.SupervisorWithDrawTrans,
}

func init() {
	Register(SupervisorWithDrawTrans)
}

func (a *supervisorWithDrawTrans) init() func() {
	return func() {}
}

// Create
func (a *supervisorWithDrawTrans) Create(ctx context.Context, in *model.SupervisorWithDrawTransCreateRequest) (int64, error) {
	c := buildSupervisorWithDrawTrans(in)
	return a.iSupervisorWithDrawTrans.Create(ctx, c)
}

// Update
func (a *supervisorWithDrawTrans) Update(ctx context.Context, in *model.SupervisorWithDrawTransUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.SupervisorWithDrawId != nil {
		dict["supervisor_with_draw_id"] = in.SupervisorWithDrawId
	}

	if in.TotalAmount != nil {
		dict["total_amount"] = in.TotalAmount
	}

	if in.Status != nil {
		dict["status"] = in.Status
	}

	if in.SupervisorId != nil {
		dict["supervisor_id"] = in.SupervisorId
	}

	if in.Delta != nil {
		dict["delta"] = in.Delta
	}

	if in.Desc != nil {
		dict["desc"] = in.Desc
	}

	if in.Operator != nil {
		dict["operator"] = in.Operator
	}

	if in.OperatorId != nil {
		dict["operator_id"] = in.OperatorId
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	if in.ExtraData != nil {
		dict["extra_data"] = in.ExtraData
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iSupervisorWithDrawTrans.Update(ctx, in.Id, dict)
}

// Delete
func (a *supervisorWithDrawTrans) Delete(ctx context.Context, in *model.SupervisorWithDrawTransDeleteRequest) error {
	return a.iSupervisorWithDrawTrans.Delete(ctx, in.Id)
}

// List
func (a *supervisorWithDrawTrans) List(ctx context.Context, in *model.SupervisorWithDrawTransListRequest) (*model.SupervisorWithDrawTransListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.SupervisorWithDrawTrans
		out   = &model.SupervisorWithDrawTransListResponse{}
	)

	if total, list, err = a.iSupervisorWithDrawTrans.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.SupervisorWithDrawTranssEntityToDto(list)

	return out, nil
}

// Find
func (a *supervisorWithDrawTrans) Find(ctx context.Context, in *model.SupervisorWithDrawTransInfoRequest) (*model.SupervisorWithDrawTransInfo, error) {
	var (
		err  error
		data *entity.SupervisorWithDrawTrans
		out  = &model.SupervisorWithDrawTransInfo{}
	)

	if data, err = a.iSupervisorWithDrawTrans.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.SupervisorWithDrawTransEntityToDto(data)
	return out, nil
}

// buildSupervisorWithDrawTrans build entity
func buildSupervisorWithDrawTrans(in *model.SupervisorWithDrawTransCreateRequest) *entity.SupervisorWithDrawTrans {

	ety := &entity.SupervisorWithDrawTrans{}

	ety.SupervisorWithDrawId = in.SupervisorWithDrawId

	ety.Status = in.Status

	ety.SupervisorId = in.SupervisorId

	ety.Delta = in.Delta

	ety.Desc = in.Desc

	ety.OperatorId = in.OperatorId

	ety.Operator = in.Operator

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
